package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formcar.CarDetailsAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.shopform.formcar.CarDetailsFindForm;
import com.woniuxy.lrz.carlproject.model.CarDetail;
import com.woniuxy.lrz.carlproject.dao.CarDetailMapper;
import com.woniuxy.lrz.carlproject.service.CarDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtocar.CarDetailsTypeBrandDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 * 车型详情表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
@Slf4j
public class CarDetailServiceImpl extends ServiceImpl<CarDetailMapper, CarDetail> implements CarDetailService {

    @Resource
    private CarDetailMapper carDetailMapper;

    @Override
    public boolean addOrUpdateCarDetail(CarDetailsAddOrUpdateForm carDetailsAddOrUpdateForm) {
            CarDetail carDetail = BeanUtil.copyProperties(carDetailsAddOrUpdateForm, CarDetail.class);
            //判断操作是新增还是修改
            CarDetail carDetail1 = carDetailMapper.selectOne(new QueryWrapper<CarDetail>().eq("displacement", carDetail.getDisplacement())
                    .eq("car_year", carDetail.getCarYear()).eq("var_version", carDetail.getVarVersion())
                    .eq("car_type_id", carDetail.getCarTypeId()));
            if(carDetail.getCarDetailId()==null){
                //新增
                if(carDetail1!=null){
                    throw new AppException("车辆信息重复，请重试！");
                }
                carDetailMapper.insert(carDetail);
            }
            //修改
            if(carDetail1!=null&&carDetail1.getCarDetailId()!=carDetail.getCarDetailId()){
                throw new AppException("车辆信息重复，请重试！");
            }
            carDetailMapper.updateById(carDetail);
        return true;
    }

    @Override
    public boolean deleteCarDetails(Integer carDetailsId) {
        //验证是否重复删除
        CarDetail carDetail = carDetailMapper.selectById(carDetailsId);
        if(carDetail==null){
            throw new AppException("请勿重复删除");
        }
        //删除
        carDetailMapper.deleteById(carDetailsId);
        return true;
    }

    @Override
    public Page<CarDetailsTypeBrandDto> findCarDetails(CarDetailsFindForm carDetailsFindForm) {
        Page<CarDetail> page=new Page<>(carDetailsFindForm.getPageNum(),carDetailsFindForm.getPageSize());
        Page<CarDetailsTypeBrandDto> carDetails = carDetailMapper.findCarDetails(page, carDetailsFindForm);
        return carDetails;
    }
}
